# Appendix K
# Santiago Lopez-Cariboni, 2022. "Political Regimes and Informal Social Insurance", Comparative Political Studies.

# ---- wd ----
wd <- 'replication_materials' 
setwd(wd)

# ---- packages ----
pkgs <- c("data.table", "foreign", 'panelView', 'fect', "lubridate", 
    "ggplot2", "dplyr", "mFilter", "texreg", "AER", "xts", "interflex", "gridExtra")
suppressPackageStartupMessages(sapply(pkgs,require,character.only=T))
# Plot the effects (use Johannes Karreth's function: https://github.com/jkarreth/JKmisc/blob/master/ggintfun.R)
# devtools::source_url("https://raw.githubusercontent.com/jkarreth/JKmisc/master/ggintfun.R")
source('code/functions/ggintfun.R')
# Function for time Lags
source('code/functions/lagpanel.R')


# ---- data ----
dt <- fread('data/dt_replication.csv')
dt <- dt[LDC==1, ]


# ---- Table 10 ----
m1 <- lm(tdlosses ~  
    + outgap.gdp.hamilton
    + democracy
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
nc1 <- length(unique(m1$model[['as.factor(iso3c)']]))
summary(m1)

m1dem <- lm(tdlosses ~
    + outgap.gdp.hamilton
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy ==1,], na.action=na.omit)
nc1dem <- length(unique(m1dem$model[['as.factor(iso3c)']]))
summary(m1dem)

m1aut <- lm(tdlosses ~  
    + outgap.gdp.hamilton
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt[democracy==0,], na.action=na.omit)
nc1aut <- length(unique(m1aut$model[['as.factor(iso3c)']]))
summary(m1aut)

m2 <- lm(tdlosses ~ 
    + outgap.gdp.hamilton * democracy
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
nc2 <- length(unique(m2$model[['as.factor(iso3c)']]))
summary(m2)


m3 <- lm(tdlosses ~ 
    + outgap.gdp.hamilton * democracy
    + Capacity
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
nc3 <- length(unique(m3$model[['as.factor(iso3c)']]))
summary(m3)


m4 <- lm(tdlosses ~ 
    + outgap.gdp.hamilton * democracy
    + Capacity
    + imports
    + exports
    + log(pop)
    + log(gdp.pcap)
    + elec.cons.pc
    + pop.density
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)
 summary(m4,  diagnostics = TRUE)
nc4 <- length(unique(m4$model[['as.factor(iso3c)']]))
summary(m4)


v.names <- c(
    "GDP Output Gap",
    "Democracy",
    "Democracy $\\times$ GDP Output Gap",
    "State Capacity",
    "Imports",
    "Exports",
    "Population (log)",
    "Real GDP per capita (log)",
    "Electricity Consumption",
    "Population Density"
    )


table1 <- texreg(list(m1dem,m1aut,m1,m2,m3,m4),
    file= "tables/OA/T10_2wfe_robust_lags_levels.tex",
    label="tab:2wfe_robust_lags_levels",
    custom.modenames = c('(Democracies)','(Autocracies)', '(All)', '(All)', '(All)', '(All)'),
    caption="Ciclicality of Electricity Losses in Autocracies and Democracies (1971 - 2014)",
    dcolumn = TRUE,
    no.margin=FALSE,
    fontsize="scriptsize",
    single.row=FALSE,
    use.packages=FALSE,
    booktabs = TRUE,
    digits=2,
    float.pos="htbp",
    sideways=FALSE,
    omit.coef= "(year)|(iso3c)|(Intercept)",
    include.rsquared = FALSE,
    stars = c(0.01, 0.05, 0.1),
    custom.gof.rows = list("Year dummies" = c("Yes", "Yes","Yes","Yes", "Yes", "Yes"), "Country fixed-effects" = c("Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
    "Number of countries"= c(nc1dem, nc1aut, nc1, nc2, nc3, nc4))
    ,
    custom.coef.names=v.names
    )




# ---- Figure K1 ----
dt <- dt[order(dt$iso3c, dt$year),] # sort for lagpanel
dt$l1.outgap.gdp.hamilton <- lagpanel(dt$outgap.gdp.hamilton, dt$iso3c, dt$year, 1) 

m1 <- lm(outgap.tdl ~  
    + outgap.gdp.hamilton
    + l1.outgap.gdp.hamilton
    + democracy
    + as.factor(iso3c)
    + as.factor(year)
     ,
    data=dt, na.action=na.omit)

library(jtools)
pdf("figures/OA/K1_lags.pdf", , width = 8, height = 6)
plot_summs(m1,
coefs = c(
    "GDP Output Gap (Lag 0)" = "outgap.gdp.hamilton", 
    "GDP Output Gap (Lag 1)" = "l1.outgap.gdp.hamilton"),
ci_level =.90)
dev.off()
